package main

import (
	"context"
	"fmt"
	stdlog "log"
	"net"

	"distributed/grades"
	"distributed/log"
	"distributed/registry"
	"distributed/service"
)

func main() {
	host, port := "localhost", "6000"
	addr := net.JoinHostPort(host, port)
	serviceAddr := fmt.Sprintf("http://%s", addr)
	reg := registry.Registration{
		ServiceName:      registry.GradingService,
		ServiceURL:       serviceAddr,
		ServiceUpdateURL: serviceAddr + "services",
		BeartbeatURL:     serviceAddr + "beartbeat",
	}
	ctx, err := service.Start(
		context.Background(), reg, addr, grades.RegisterHandlers,
	)
	if err != nil {
		stdlog.Fatalln(err)
	}
	logServices, err := registry.GetProviders(registry.LogService)
	if err == nil {
		stdlog.Printf("Logging service found at: %v", logServices)
		log.SetClientLogger(logServices[0], reg.ServiceName)
	}
	<-ctx.Done()
	fmt.Println("Shutting down grading service.")
}
